const httpclient = require('../../utils/httpclient.js')
//获取应用实例
var app = getApp()
Page({
  data: {
    addressList:[]
  },

  selectTap: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    // 设置该ID的收货信息为默认地址
    httpclient.req(
      "/mall/set_default_consignee",
      {
        user_id: app.globalData.userInfo.id,
        id: id
      },
      'post',
      function (res) {
        if(res.data.result){
          that.initShippingAddress();
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },

  addAddess : function () {
    wx.navigateTo({
      url:"/pages/address-add/index"
    })
  },

  delAddess:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          // 设置该ID的收货信息为默认地址
          httpclient.req(
            "/mall/del_consignee_one",
            {
              id: id
            },
            'post',
            function (res) {
              if (res.data.result) {
                that.initShippingAddress();
              }
            },
            function (result) {
              console.log(result)
            }
          );
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },
  
  onLoad: function () {
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  onShow : function () {
    this.initShippingAddress();
  },
  initShippingAddress: function () {
    var that = this;
    httpclient.req(
      "/mall/query_user_consignee_info",
      {
        user_id: app.globalData.userInfo.id
      },
      'post',
      function (res) {
        that.setData({
          addressList: res.data
        });
      },
      function (result) {
        console.log(result)
      }
    );
  }

})
